</div>
<div id="footer"><a href="http://www.mydomain.com">mydomain.com</a> &copy; 2009-2012 All Rights Reserved.</div>
</body>
</html>
<script type="text/javascript">
// 格式化数字字符，保留小数位
String.prototype.numberFormat = function(decimals){
    if (isNaN(this)) { return 0; };
    if (this == '') { return 0; };
    var sec = this.split('.');
    var whole = parseFloat(sec[0]);
    var result = '';
    if (sec.length > 1) {
        var dec = new String(sec[1]);
        dec = String(parseFloat(sec[1]) / Math.pow(10, (dec.length - decimals)));
        dec = String(whole + Math.round(parseFloat(dec)) / Math.pow(10, decimals));
        var dot = dec.indexOf('.');
        if (dot == -1) {
            dec += '.';
            dot = dec.indexOf('.');
        }
        while (dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    } else {
        var dot;
        var dec = new String(whole);
        dec += '.';
        dot = dec.indexOf('.');
        while (dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    }
    return result;
};

// 复制文本到剪贴板
function copyToClipboard(txt) {
    if(window.clipboardData) {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text", txt);
    } else if(navigator.userAgent.indexOf("Opera") != -1) {
        window.location = txt;
    } else if (window.netscape) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
            if (!clip) return false;
            var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
            if (!trans) return false;
            trans.addDataFlavor('text/unicode');
            var str = new Object();
            var len = new Object();
            var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
            var copytext = txt;
            str.data = copytext;
            trans.setTransferData("text/unicode", str, copytext.length * 2);
            var clipid = Components.interfaces.nsIClipboard;
            if (!clip)  return false;
            clip.setData(trans, null, clipid. kGlobalClipboard);
        } catch (e) {
            alert("被浏览器拒绝！请在浏览器地址栏输入'about:config'并回车然后将'signed.applets.codebase_principal_support'设置为'true'");
            return false;
        }
    }
    return true;
}

// AJAX POST 处理
$.fn.ajax_post = function(params) {
    var options = {
        url : $(this).attr('action') ? $(this).attr('action') : document.location.href,
        type : 'post',
        data : $(this).serialize(),
        dataType : 'html',
        cache : false,
        redirect : false,
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            $('#content .box').prevAll('.warning, .errors, .success').remove();
            $('#content .box').before('<div class="warning">可能发生了错误导致提交失败，错误信息：' + (errorThrown?errorThrown:textStatus) + '</div>');
        },
        success : function(html){
            if (/^\s*(success|true|1)?\s*$/.test(html)){
                options.redirect ? document.location.href = options.redirect : document.location.reload();
            } else {
                $('#content .box').prevAll('.warning, .errors, .success').remove();
                $('#content .box').before(html);
            }
        }
    };
    $.ajax($.extend(options, params));
    return false;
};

// AJAX GET 处理
$.ajax_get = function(params) {
    var options = {
        url : document.location.href,
        type : 'get',
        dataType : 'html',
        cache : false,
        redirect : false,
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            $('#content .box').prevAll('.warning, .errors, .success').remove();
            $('#content .box').before('<div class="warning">可能发生了错误导致操作失败，错误信息：' + (errorThrown?errorThrown:textStatus) + '</div>');
        },
        success : function(html){
            if (/^\s*(success|true|1)?\s*$/.test(html)){
                options.redirect ? document.location.href = options.redirect : document.location.reload();
            } else {
                $('#content .box').prevAll('.warning, .errors, .success').remove();
                $('#content .box').before(html);
            }
        }
    };
    $.ajax($.extend(options, params));
    return false;
};

$.fn.field_toggle = function(url, id, field) {
    var self = $(this).get(0);
    var value = (self.src.match(/yes.gif/i)) ? 0 : 1;
    $.ajax({
        url : url,
        data : { id : id, field : field, value : value},
        type : 'get',
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert('可能发生了错误导致操作失败，错误信息：' + (errorThrown?errorThrown:textStatus));
        },
        success : function(html){
            if (html.length > 0) {
                alert(html);
            } else if (value == 0) {
                self.src = self.src.replace('yes.gif', 'no.gif');
            } else {
                self.src = self.src.replace('no.gif', 'yes.gif');
            }
        }
    });
};

// 提交筛选条件
$.filter_submit = function() {
    var url = location.toString().replace(/\#[\w\.\-]+/, '');
    if (/\?\w+/.test(location.toString())) {
        $('tr.filter').find('select, textarea, input').not('input[type=button],input[type=submit]').each(function(i, ele){
            var regex = new RegExp(ele.name + '=[^&]*', 'i');
            if (regex.test(url)) {
                url = url.replace(regex, ele.name + '=' + encodeURIComponent(ele.value));
            } else {
                url += '&' + ele.name + '=' + encodeURIComponent(ele.value);
            }
        });
    } else {
        url += '?' + $('tr.filter').find('select, textarea, input').not('input[type=button],input[type=submit]').serialize();
    }
    location = url;
};

// 清除筛选条件
$.filter_clean = function(){
    $('tr.filter').find('select, textarea, input').not('input[type=button],input[type=submit]').val('');
};

// 显示对话框
function show_dialog(element, params) {
    $(element).dialog($.extend({
        bgiframe: true,
        width: 750,
        height: 400,
        resizable: false,
        modal: true
    }, params));
    if (typeof params.url == 'string') {
        $(element).load(params.url);
    }
};

// 对话框 AJAX 提交
$.fn.dialog_ajax_post = function(params) {
    var options = {
        url : $(this).attr('action') ? $(this).attr('action') : document.location.href,
        type : 'post',
        data : $(this).serialize(),
        dataType : 'html',
        cache : false,
        redirect : false,
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert('可能发生了错误导致提交失败，错误信息：' + (errorThrown?errorThrown:textStatus));
        },
        success : function(html){
            if (/^\s*(success|true|1)?\s*$/.test(html)) {
                options.redirect ? document.location.href = options.redirect : document.location.reload();
            } else {
                alert(html);
            }
        }
    };
    $.ajax($.extend(options, params));
    return false;
};

$().ready(function(){
    $('#menu > ul').superfish({
        hoverClass   : 'sfHover',
        pathClass    : 'overideThisToUse',
        delay        : 0,
        animation    : {height: 'show'},
        speed        : 'normal',
        autoArrows   : false,
        dropShadows  : false,
        disableHI    : false, /* set to true to disable hoverIntent detection */
        onInit       : function(){},
        onBeforeShow : function(){},
        onShow       : function(){},
        onHide       : function(){}
    });
    $('#menu > ul').css('display', 'block');

    // 回车提交输入表单
    $('form input').keydown(function(e) {
        if (e.keyCode == 13) {
            $('#form').submit();
        }
    });

    // 删除确认
    $('a.delete').click(function(){
        return confirm('删除后将无法恢复，请确定要这么做吗?');
    });

    // 下拉提交搜索表单
    $('tr.filter').find('select').change(function(){
        $.filter_submit()
    });
    // 回车提交搜索表单
    $('tr.filter').find('input').keydown(function(e) {
        if (e.keyCode == 13) {
            $.filter_submit();
        }
    });

    // 回车提交对话框表单
    $('div.dialog form').each(function(i, form){
        $(form).find('select, input').keydown(function(e) {
            if (e.keyCode == 13) {
                return $(form).dialog_ajax_post();
            }
        });
    });

    <? if (isset($autosort)) { ?>
    $('.autosort[name=\'<?=$autosort->field?>\']').addClass('autosort-<?=$autosort->direct?>').append('<img src="<?=URL::base()?>images/admin/arrow_<?=($autosort->direct == 'asc')?'down':'up'?>.gif" style="margin-left:2px" />');
    $('table .autosort-desc, table .autosort-asc, table .autosort').click(function(){
        var url = location.toString().replace(/\#[\w\.\-]+/, '');
        var field = $(this).attr('name');
        var direct = (field == '<?=$autosort->field?>') ? '<?=$autosort->direct=='asc'?'desc':'asc'?>' : '<?=$autosort->direct?>';
        if (/\?\w+/.test(url)) {
            url = /field=[\w\.]+/i.test(url) ? url.replace(/field=[\w\.]+/i, 'field=' + field) : url + '&field=' + field;
            url = /direct=[\w\.]+/i.test(url) ? url.replace(/direct=[\w\.]+/i, 'direct=' + direct) : url + '&direct=' + direct;
        } else {
            url += '?field=' + field + '&direct=' + direct;
        }
        location = url;
    });
    <? } ?>

    $('table.list tbody tr:odd > td').css('background-color', '#FFFFFF');
    $('table.list tbody tr:even > td').css('background-color', '#FAFCFF');
    $('table.list tbody tr').mouseover(function(){
        $(this).data('color', $(this).find('td:first').css('background-color'));
        $(this).find('td').css('background-color', '#FAEFE0');
    }).mouseout(function(){
        $(this).find('td').css('background-color', $(this).data('color'));
    });
});
</script>
